/* eslint-disable vue/no-use-v-if-with-v-for */
<template>
  <div>
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.go(-1)"
      />
      <!-- 搜索组件 -->
      <van-search
        v-model.trim="kw"
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        v-focus
        ref="search"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div class="sugg-list" v-if="kw.length !== 0">
      <div
        class="sugg-item"
        v-for="(item, index) in suggestionList"
        :key="index"
        v-html="hightSearchName(item)"
        @click="$router.push({ path: `/SearchResult/${item}` })"
      ></div>
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="clearFn"/>
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span
          class="history-item"
          v-for="(str, index) in history"
          :key="index"
          @click="historyBtn(str)"
          >{{ str }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import { suggestionApi } from '@/api'
import { setStorage, getStorage } from '@/storage/local'
export default {
  data () {
    return {
      kw: '', // 搜索关键字
      timer: '', // 防抖定时器
      suggestionList: [], // 联想菜单数据
      history: JSON.parse(getStorage('history')) || [] // 搜索历史
    }
  },
  watch: {
    history: {
      deep: true, // 深度侦听(history侦听变量本身的改变, 例如: history=[])
      handler () {
        // setStorage('history', JSON.stringify(this.history))
        setStorage('history', JSON.stringify(this.history))
      }
    },
    kw () {
      clearTimeout(this.timer)
      if (this.kw.length === 0) {
        this.suggestionList = []
        return
      }
      this.timer = setTimeout(async () => {
        if (this.history.indexOf(this.kw) === -1) { this.history.push(this.kw) }
        const [, res] = await suggestionApi({ q: this.kw })
        this.suggestionList = res.data.data.options
      }, 1000)
    }
  },
  methods: {
    hightSearchName (item) {
      const reg = new RegExp(this.kw, 'ig')
      return item.replace(reg, (val) => {
        // val是正则匹配的字符
        return `<span style="color: red">${val}</span>` // return的结果会替换掉参数1在item里匹配到的文字
      })
    },
    historyBtn (str) {
      // 点击填充到输入框里
      this.kw = str
      this.$refs.search.querySelector('input').focus()
    },
    clearFn () {
      // 清空历史记录
      this.history = []
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
